<?
// Page variables
$rightCol   = 1;
$parseURI   = 1;
$loggedUser = 0;
$myShop     = 0;
$ads        = 0;

$path       = $_GET[path];

include("includes/functions.php"); 

if ($_GET[submit] == 1) {
	// Clean input
	$fullName    = fieldToDB($_POST[fullName]);
	$email       = fieldToDB($_POST[email]);
	$title       = emailPreventInjection(fieldToDB($_POST[title]));
	$url         = emailPreventInjection(fieldToDB($_POST[url]));
	$description = fieldToDB($_POST[description]);
	$path        = $_POST[resourceCat];
	$test        = $_POST[test];
	
	// Check required	
	if ( empty($test) || (strtolower($test) != 'network') ) {
		$error = 1;
		$message  = 'Sorry! You have incorrectly answered the Test Question. Try again.';
	}
	
	if (empty($email)) {
		$error = 1;
		$message = "Please enter your email address.";
	}
	if (empty($url)) {
		$error = 1;
		$message = "Please enter the link URL or website address for this resource.";
	}
	if ($error != 1) {
		$sql = "INSERT into transLinks (
				Path,
				Title,
				LinkDescription,
				Link,
				UserID,
				UserName,
				DateCreated,
				Status
				) values (
				'$path',
				'$title',
				'$description',
				'$url',
				'$email',
				'$fullName',
				'".date('Y-m-d')."',
				'0'
				)";
		$result = mysql_db_query($dbname,$sql);
		
		// Send mail to Admin
		mailAdmin("newResource",$fullName,$email,$title,$url);
		$sent = 1;
	}	
}

include("includes/header.php"); ?>
<Script Language="JavaScript">
<!-- 
function checkForm(form) {
	var testString = form.test.value;
	var testString = testString.toLowerCase();
	if (form.email.value == "") {
		alert("Please enter your email address.")
		return false		
	}
	if ( (form.url.value == "") || (form.url.value == "http://") ) {
		alert("Please enter the URL for this resource.")
		return false		
	}	
	if ( (testString == "") || (testString != "network") ) {
		alert("Sorry! You have incorrectly answered the Test Question. Try again.")
		return false		
	}	
	
	return true;
} 
function gotoURL(urlID) {
	var url = document.getElementById(urlID).value;
	window.open(url);
}
//-->
</script>

	<div id="pageTop"></div>
	<div id="pageContent">
		<? include("includes/menu_main.php"); ?>
		
		<div id="rightCol">
			<img src="images/rightColTop.gif">
			<? include("includes/rightCol.php");  ?>
		</div>
		
		<div id="mainContent">
			<h1>Add a Resource to the Network</h1>
			
			<p class="margin">
			<? if ($submit == 1) { ?>
			<b><?=$fullName; ?>, your resource has been added to the network.</b><br><br>
			
			Thank you for your contribution to the Transition Community Network. Your involvement helps
			make this website a valuable online resource for everyone. Your resource will appear on
			the website once it has been approved by our moderators.<br><br> 
			
			<div class="spacer2"></div>
			<? } else { ?>
			Please complete the form below to submit a resource to the Transition Community Network.
			Your contribution will appear on the website once it has been viewed and approved by our
			moderators.<br><br>
			
			<form action="<?=$PHP_SELF; ?>?submit=1" method="POST" name="main" onSubmit="return checkForm(this)">
			<input type="hidden" name="path" value="<?=$_POST[path]; ?>">
			<table border="0" width="100%" cellspacing="2">			
			<tr>
				<td bgcolor="#E8E8D7" width="40%" class="formText"><font color="#E8E8D7">&raquo;</font> Your Name</td>
				<td>&nbsp;<input type="text" name="fullName" size="30" value="<?=$fullName; ?>"></td>
			</tr>
			<tr>
				<td bgcolor="#E8E8D7" valign="top" class="formText"><font color="red">&raquo;</font> Your Email Address</td>
				<td class="text">&nbsp;<input type="text" name="email" size="30" value="<?=$email; ?>">
				</td>
			</tr>
			<tr>
				<td bgcolor="#E8E8D7" valign="top" class="formText"><font color="#E8E8D7">&raquo;</font> Resource Title</td>
				<td class="text">&nbsp;<input type="text" name="title" size="30" value="<?=$title; ?>">
				</td>
			</tr>
			<tr>
				<td bgcolor="#E8E8D7" valign="top" class="formText"><font color="red">&raquo;</font> Resource Link URL</td>
				<td class="text">&nbsp;<input type="text" id="url" name="url" size="30" value="http://">
				&nbsp;<a href="#" onClick="gotoURL('url'); return false;">Test Link</a>
				</td>
			</tr>
			<tr>
				<td bgcolor="#E8E8D7" valign="top" class="formText"><font color="red">&raquo;</font> Resource Category</td>
				<td class="text">&nbsp;<select name="resourceCat" size="1">
				<?
				// Get main categories
				$sql = "SELECT * from transLevels WHERE MainCat = '1' AND InMainMenu = '1' ORDER BY DisplayOrder";
				$result = mysql_db_query($dbname,$sql);
				$a=0;
				while ($value = mysql_fetch_assoc($result)) {
					$levelID    = $value[LevelID];
					$levelName  = stripslashes($value[LevelName]);
					$path       = $value[Path];
					echo "<option value=\"$path\"";
					if ($path == $_GET[path]) { echo " selected "; }
					echo ">$levelName</option>\n";	
				}	
				?>
				</select>
				</td>
			</tr>
			
			</table>
			<table border="0" cellspacing="2" cellpadding="3">			
			<tr>
				<td bgcolor="#E8E8D7" width="100%" class="formText">Resource Description</td>
			</tr>
			<tr>
				<td>Please give a brief description of the link you are submitting.<br>
				<textarea name="description" style="width: 400px; height: 150px;"></textarea></td>
			</tr>
			<tr>
				<td bgcolor="#E8E8D7" width="100%" class="formText">Test Question (In order to prevent spam)</td>
			</tr>
			<tr>
				<td>What is the 3rd word in the name of this website?<br>
				<input type="text" id="test" name="test" style="width: 390px;" /></td>
			</tr>
			<tr>
				<td align="right" width="390"><input type="image" src="images/buttonSubmitResource.gif" style="border: none;" alt="Submit Resource"></td>
			</tr>
			</table>
			</form>
			<? } ?>
			<div class="spacer2" style="height: 400px;"></div>
		</div>
		
	</div>
<? include("includes/footer.php"); ?>